package com.hands.sample.handlers;

//import com.alibaba.fastjson.JSONObject;
import com.amazon.ask.dispatcher.request.handler.HandlerInput;
import com.amazon.ask.dispatcher.request.handler.RequestHandler;
import com.amazon.ask.model.*;
import net.sf.json.JSONObject;

import java.util.Map;
import java.util.Optional;

import static com.amazon.ask.request.Predicates.intentName;

public class HelloWorldIntentHandler implements RequestHandler {

    @Override
    public boolean canHandle(HandlerInput input) {
        return input.matches(intentName("HelloWorldIntent"));
    }

    @Override
    public Optional<Response> handle(HandlerInput input) {

        Request request = input.getRequestEnvelope().getRequest();
        IntentRequest intentRequest = (IntentRequest) request;
        Intent intent = intentRequest.getIntent();
        Map<String, Slot> slots = intent.getSlots();
        String slotinfo = slots.get("SlotName").getValue();
        JSONObject data = JSONObject.fromObject(slotinfo);
        String A1s = data.getString("answer");
        JSONObject parameters = data.getJSONObject("parameters");

        /*Title title = Title.builder().withMainTitle("").withSubTitle("").build();
        if (parameters != null) {
            String date = parameters.getString("date");
            String city = parameters.getString("city");
            title = Title.builder().withMainTitle(city).withSubTitle(date).build();
        } else {
            System.out.println("///// parameters is null /////");
        }*/

       // log.writeFile("C://001.log","执行hellow world，返回结果：" + parameters+":date:"+parameters.getString("date")+":city:"+ parameters.getString("city"));
        String speechText = "Hello world";
       return input.getResponseBuilder()
                .withSpeech(speechText)
                .withSimpleCard("HelloWorld", speechText)
                .build();
    }

}
